#include "login.h"
#include "ui_login.h"
#include <QMessageBox>
#include <QHostInfo>
#include <QDebug>
Login::Login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
    connect(ui->loginpushButton, SIGNAL(clicked(bool)), this, SLOT(on_loginpushButton_clicked()));
    port=10000;
    tcpClient=new QTcpSocket(this);
    QString LocalIP=getLocalIP();
    connect(tcpClient,SIGNAL(connected()),this,SLOT(onConnected()));
    connect(tcpClient,SIGNAL(disconnected()),this,SLOT(onDisconnect()));
    //连接服务器
    quint16 targetport=8888;
    tcpClient->connectToHost(LocalIP,targetport);
    qDebug() <<tcpClient->state();
    //绑定接收服务器消息的槽函数
    connect(tcpClient, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));

}

Login::~Login()
{
    delete ui;
}

QString Login::getLocalIP()
{
   QString hostName=QHostInfo::localHostName();
   QHostInfo hostInfo=QHostInfo::fromName(hostName);
   QString localIP="";
   QList<QHostAddress> addList=hostInfo.addresses();
   if(!addList.isEmpty())
   {
       for(int i=0;i<addList.count();i++)
       {
           QHostAddress aHost=addList.at(i);
           if(QAbstractSocket::IPv4Protocol==aHost.protocol())
           {
               localIP=aHost.toString();
               break;
           }
       }
   }
   return localIP;
}

void Login::processPendingDatagrams()
{
    QByteArray datagram;
    datagram=tcpClient->readAll();
    QDataStream in(&datagram,QIODevice::ReadOnly);
    quint8 type;
    QString info;
    in>>type;
    //
    if(type=='D')//数据库出错
    {
        in>>info;
        QMessageBox::warning(0,tr("警告"),"数据库出错"+info,QMessageBox::Ok);
    }
    else if(type=='P') //通过
    {
         in>>info;
         QMessageBox::information(0,tr("提示"),"登录成功",QMessageBox::Ok);
         showChatWidget();
    }
    else if(type=='E')//密码出错
    {
         QMessageBox::warning(0,tr("警告"),"密码错误",QMessageBox::Ok);
    }
    else if(type=='N')//数据库出错
    {
         in>>info;
         QMessageBox::warning(0,tr("警告"),"用户不存在:"+info,QMessageBox::Ok);
    }
    else
    {
        QMessageBox::warning(0,tr("警告"),"未知错误",QMessageBox::Ok);
    }
}

void Login::showChatWidget()
{
    chatWidget = new Widget(0,ui->namelineEdit->text());
    chatWidget->setWindowTitle(ui->namelineEdit->text());
    chatWidget->setAttribute(Qt::WA_DeleteOnClose);//这句话一定要加，否则widget关掉后不会释放
    chatWidget->show();
    chatWidget->on_refresh_pushButton_clicked();
}

void Login::showRegisterWidget()
{
    registerWidget=new Register();
    registerWidget->setAttribute(Qt::WA_DeleteOnClose);
    registerWidget->show();
}

void Login::on_loginpushButton_clicked()
{
    QByteArray data;
    QDataStream out(&data, QIODevice::WriteOnly);
    QString username=ui->namelineEdit->text();
    QString password=ui->passwordlineEdit->text();
   //提交登录的信息
    out<< quint8('L')<<username<<password;
    tcpClient->write(data);//发送数据
}


void Login::on_registerpushButton_clicked()
{
    //打开一个新的注册的界面，要求点击提交后会自动关闭
    showRegisterWidget();
}

void Login::onConnected()
{
    QMessageBox::information(0,tr("提示"),tr("成功连接到服务器"),QMessageBox::Ok);
}

void Login::onDisconnect()
{
 QMessageBox::warning(0,tr("警告"),tr("无法连接服务器"),QMessageBox::Ok);
}

